﻿using Rougamo.Context;
using Rougamo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AOP.Rougamo
{
    // 1.定义类继承MoAttribute，在该类中定义你在方法执行各阶段需要织入的代码
    public class LoggingAttribute : MoAttribute
    {

        public string FlowName { get; set; }
        public LoggingAttribute(string flowName)
        {
            FlowName = flowName;
        }

        // 改为所有public方法有效，不论是实例方法还是静态方法
        public override AccessFlags Flags => AccessFlags.Public;

        public override void OnEntry(MethodContext context)
        {
            // 从context对象中能取到包括入参、类实例、方法描述等信息
            Console.WriteLine($"节点{FlowName}方法开始执行");
            var reqArg = context.Arguments;


            if (reqArg != null)
                Console.WriteLine($"获取到节点{FlowName}请求参数为{string.Join(",", reqArg)}");
            Task.Run(() => Dispatched.TestA());
        }

        public override void OnException(MethodContext context)
        {
            Console.WriteLine($"节点{FlowName}方法执行异常", context.Exception);
        }

        public override void OnSuccess(MethodContext context)
        {
            var ReturnValue = context.ReturnValue;
            if (ReturnValue != null)
                Console.WriteLine($"获取到节点{FlowName}执行结果为{ReturnValue}");
            Console.WriteLine($"节点{FlowName}方法执行成功");
        }

        public override void OnExit(MethodContext context)
        {
            Console.WriteLine($"节点{FlowName}方法退出时，不论方法执行成功还是异常，都会执行");
        }
    }





}
